import java.sql.*

if (args.length < 4) {
    println "Usage: groovy "+getClass().getName()+" [distribution] [removed-packages-list] [output-sql-file] [log-file]"
    println "Example: groovy "+getClass().getName()+" cooker ~/mephisto/data/cooker/i586-main-release.removed.list ~/mephisto/data/cooker/i586-main-release.removal.sql ~/logs/mphs.log"
    return
}

distrib = args[0]
sqlFile = new File(args[2])
sqlFile.setText("")
log = new File(args[3])

log.append("\n== Generating diff packages sql command ==\n")

oldPackageList = new File(args[1]).text.split()

if (oldPackageList.size()>0) {
    for (pkg in oldPackageList) {
        //distribution='${distrib}' and 
        sqlFile.append("DELETE FROM PACKAGES WHERE package='${pkg}';\n")
        sqlFile.append("DELETE FROM PACKAGE_DEPENDENCIES WHERE package='${pkg}';\n")
        sqlFile.append("DELETE FROM PACKAGE_FILES WHERE package='${pkg}';\n")
    
    }
    
    println "Generated: $sqlFile - ok."
    log.append("Generated: $sqlFile - ok.\n")
} else {
    println "No data to be removed"
    log.append("No data to be removed.\n")
}

//p = "/usr/bin/mysql -u ${sqluser} -p${sqlpasswd} mephisto < ${sqlFilePath}".execute()
//println "Output: "+p.text
